package org.mogu.control;

import org.mogu.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

/*
 * 自定义的头像显示控件
 * @author androidSource@139.com
 * */
public class CustomedHeadShow extends View{
  final int a;
  final int b;
  final int c;
  final int d;
  private Drawable defaultHead;//头像
  private Drawable boxHead;//显示头像的框
  private Paint mPaint;//画笔对象
  private float size;//元素尺寸

  /*
   * 构造函数
   * */
  public CustomedHeadShow(Context paramContext){
    this(paramContext, null);
  }

  /*
   * 构造函数
   * paramAttributeSet-布局文件参数
   * */
  public CustomedHeadShow(Context paramContext, AttributeSet paramAttributeSet){
    super(paramContext, paramAttributeSet);
    
    //创建画笔类
    Paint localPaint = new Paint();
    mPaint = localPaint;
    size = 1.0F;
    this.a = 0;
    this.b = 0;
    this.c = 9;
    this.d = 4;
    
    //获取默认头像资源文件
    Drawable localDrawable1 = paramContext.getResources().getDrawable(R.drawable.moguico);
    defaultHead= localDrawable1;
    
    //获取头像文件的框资源文件
    Drawable localDrawable2 = paramContext.getResources().getDrawable(R.drawable.topface_back);
    boxHead = localDrawable2;
    
    //调整元素尺寸
    float f1 = paramContext.getResources().getDisplayMetrics().density;
    size = f1;
  }

  //设置头像框的资源文件
  public void setDrawableBox(Drawable paramDrawable){
	  boxHead = paramDrawable;
  }

  //设置头资源文件
  public void setDrawableDefault(Drawable paramDrawable){
	 defaultHead = paramDrawable;
     invalidate();
  }

  /*
   * 控件的绘图函数
   * */
  protected void onDraw(Canvas paramCanvas){
	    super.onDraw(paramCanvas);//继承父类的绘图函数
	    
	    //保存当前视图
	    int i = paramCanvas.save();
	    
	    /*
	     * 调整好绘图位置
	     * */
	    //头像框
	    Drawable localDrawable1 = boxHead;
	    int j = (int)(size * 0.0F + 0.5F);
	    int k = (int)(size * 0.0F + 0.5F);
	    int l = boxHead.getIntrinsicWidth() + 0;
	    int i1 = boxHead.getIntrinsicHeight() + 0;
	    localDrawable1.setBounds(j, k, l, i1);
	    //绘制框
	    boxHead.draw(paramCanvas);
	    
	    /*
	     * 调整好绘图位置
	     * */
	    //头像
	    Drawable localDrawable2 = defaultHead;
	    float f1 = size;
	    int i2 = (int)(9.0F * f1 + 0.5F);
	    float f2 = size;
	    int i3 = (int)(4.0F * f2 + 0.5F);
	    float f3 = size;
	    int i4 = (int)(37.0F * f3 + 0.5F);
	    float f4 = size;
	    int i5 = (int)(32.0F * f4 + 0.5F);
	    localDrawable2.setBounds(i2, i3, i4, i5);
	    //绘制头像
	    defaultHead.draw(paramCanvas);
	    
	    //恢复视图
	    paramCanvas.restoreToCount(i);
  }
}
